
	** file to identify wind direction that is the most polluting for each calendar month and zip2 
	
	***Inputs: 
	* $Data/GameUserWPdaily_norm.dta
	
	***Outputs: 
	* $Data/maxmindirectionsmonth_zip260_coefs.dta
	* $Data/maxmindirectionsmonth_zip260.dta
	
	
	use  $Data/GameUserWPdaily_norm.dta, clear 

	keep norm_score anon_id day_of_week date weighted_dailypm age median_closestwind zip 	

	gen monthyear = mofd(date)

	gen highpm =  weighted_dailypm > 25 & weighted_dailypm<. 

	egen cut_winddirection = cut(median_closestwind), at(0(60)360)
	replace cut_winddirection = 0 if median_closestwind ==360	
	
	gen zip2 = int( zip/10)
	levelsof zip2
	local zip2 = r(levels)
	
	gen month = month(date)
	
	foreach zip in `zip2' {
		
		
	quietly	reghdfe highpm  i.cut_wind#i.month  if zip2 ==`zip' ,   absorb( anon_id  monthyear) cluster(anon_id monthyear )
				
		regsave using $Results/zipwind60month`zip', replace 
		
				
	}
	
	use $Results/zipwind60month99, clear
	
	gen zip2 = 99
	
		forval zip = 1(1)98 {
		
		capture append using  $Results/zipwind60month`zip'
		
		replace zip2 = `zip' if zip2==.
		
		
	}
	

	
	
	
	gen direction = substr(var,1,3)
	destring direction, replace force ignore("c" "."  "u" "ons" "b")

	gen month = substr(var,-9,3)
	destring month, replace force ignore("c" "."  "u" "o" "b" "#" "n")

	
	replace coef = . if r2==. // 
	
	by zip2 month, sort: egen maxcoef= max(coef) if direction<.  & coef <. 
	by zip2 month, sort: egen mincoef= min(coef) if direction<.  & coef <. 


	gen maxdirection = direction if maxcoef ==coef & maxcoef >=0  & maxcoef<. 
	gen mindirection = direction if mincoef ==coef & mincoef <=0  & mincoef <.
	
	save $Data/maxmindirectionsmonth_zip260_coefs.dta, replace
	
	collapse (firstnm) maxdirection mindirection , by(zip2 month)
	
	drop if month ==. 
		
	save $Data/maxmindirectionsmonth_zip260.dta, replace
	
	

	
	
	
	
